package GUI;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.LinkedList;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;

import voting.ClientVoting;


import network.Client;
import network.Operations;

import entities.Candidate;
import entities.Voting;

public class VotingResultsPanel extends JPanel implements ActionListener{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	JLabel title;
	JLabel results;
	JTable resultsTable;
	JScrollPane resultsScroll;
	
	
	Voting voting;
	
	Candidate[] candidates;
	
	JButton backButton;
	
	boolean isAdmin;
	public VotingResultsPanel(Voting voting,boolean isAdmin)
	{
		
		this.isAdmin=isAdmin;
		setBounds(0,0,700,500);
		setLayout(null);
		this.voting=voting;
			
		title= new JLabel(voting.getQuestions());
		title.setBounds(20,20,100,30);
		
		results= new JLabel("Voting results:");
		results.setBounds(20,60,100,30);
		initTable();
		
		resultsScroll=new JScrollPane(resultsTable);
		resultsScroll.setBounds(20,100,400,200);
		
		backButton=new JButton("Back to Votings");
		backButton.setBounds(100,350,170,30);
		backButton.addActionListener(this);
		
		add(title);
		add(results);
		add(resultsScroll);
		add(backButton);
	}
	public void getVoting()
	{
	
		voting=ClientVoting.viewVoting(voting.getVotingID(), voting.getAdminID());
		candidates=ClientVoting.candidates;
		
	}
	
	public void initTable()
	{
		
		getVoting();
		LinkedList<String> result=DecimalToBase(new BigInteger(voting.getResult()),new BigInteger((voting.getNumberOfVoters()+1)+""));
		Object[][] data=new Object[candidates.length][2];
		int j=0;
		System.out.println(result.size());
//		System.out.println("Result "+result.get(0)+" "+result.get(1));
		for(int i=result.size()-1;i>=0;i--)
		{
			data[j][0]=candidates[j].getName();
			data[j][1]=result.get(i);
			j++;
		}
		for(;j<candidates.length;j++)
		{
			data[j][0]=candidates[j].getName();
			data[j][1]=0;
		}
		
		resultsTable=new JTable(data,new String[]{"Candidate","Number Of Votes"});
	}
	/*
	 *TODO testing
	 */
	public static LinkedList<String> DecimalToBase(BigInteger b, BigInteger base)
	{
	    /*
	    BigInteger[] result = new BigInteger[iDec.bitLength()+1];
	    int MaxBit = result.length-1;
	    
	    for(; !iDec.equals(BigInteger.ZERO); iDec=iDec.divide(numbase))
	    {
	    	System.out.println(iDec);
	        BigInteger rem = iDec.mod(numbase);
	        System.out.println("reminder "+rem);
	        result[MaxBit--] = rem;
	    } 
	    BigInteger [] newResult=new BigInteger[result.length-MaxBit-1];
	    
	    for(int i=0;i<newResult.length;i++)
	    {
	    	newResult[i]=result[MaxBit];
	    	MaxBit++;
	    	
	    }
	    
	    return newResult;
	    */
		LinkedList <String> result = new LinkedList <String> ();
		while(!b.equals(BigInteger.ZERO))
		{
			result.addFirst(b.mod(base).toString());
			b = b.divide(base);
		}
		return result;
	}
	public static void main(String[]args)
	{
		/*JFrame frame=new JFrame();
		frame.setBounds(0,0,750,550);
		Voting v=new Voting();
		v.setAdminID(1);
		v.setVotingID(13);
		frame.setContentPane(new VotingResultsPanel(v,frame));
		frame.setVisible(true);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);*/
		
		
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource()==backButton)
		{
			MainFrame.getMainFrame().remove(this);
			MainFrame.getMainFrame().addViewVotingPnl(isAdmin);
		}
		
	}
   
}
